﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Abp.Application.Services.Dto;
using Abp.AspNetCore.Mvc.Authorization;
using DualControl.Authorization;
using DualControl.Controllers;
using DualControl.Users;
using DualControl.Web.Models.Users;
using DualControl.Users.Dto;
using X.PagedList;

namespace DualControl.Web.Controllers
{
    [AbpMvcAuthorize(PermissionNames.Pages_Users)]
    public class UsersController : DualControlControllerBase
    {
        private readonly IUserAppService _userAppService;

        public UsersController(IUserAppService userAppService)
        {
            _userAppService = userAppService;
        }

        //public async Task<ActionResult> Index()
        //{
        //    var users = (await _userAppService.GetAll(new PagedResultRequestDto {MaxResultCount = int.MaxValue})).Items; // Paging not implemented yet
        //    var roles = (await _userAppService.GetRoles()).Items;
        //    var model = new UserListViewModel
        //    {
        //        Users = users,
        //        Roles = roles
        //    };
        //    return View(model);
        //}

        //public async Task<ActionResult> EditUserModal(long userId)
        //{
        //    var user = await _userAppService.Get(new EntityDto<long>(userId));
        //    var roles = (await _userAppService.GetRoles()).Items;
        //    var model = new EditUserModalViewModel
        //    {
        //        User = user,
        //        Roles = roles
        //    };
        //    return View("_EditUserModal", model);
        //}

        public async Task<ActionResult> Index(int page)
        {
            int pagenumber = page < 1 ? 1 : page;
            //var users = (await _userAppService.GetAll(new PagedResultRequestDto {MaxResultCount = int.MaxValue})).Items; // Paging not implemented yet

            GetUsersInput input = new GetUsersInput
            {
                SkipCount = (pagenumber - 1) * AppConsts.DefaultPageSize,
                MaxResultCount = AppConsts.DefaultPageSize
            };
            var users = (await _userAppService.GetUsers(input)); // Paging not implemented yet

            var TotalCount = await _userAppService.GetUserCount();

            var PageOfUserListDto = new StaticPagedList<UserDto>(users.Items, pagenumber, AppConsts.DefaultPageSize, TotalCount);

            //将分页结果放入ViewBag供View使用
            ViewBag.PageOfUserListDto = PageOfUserListDto;


            var roles = (await _userAppService.GetRoles()).Items;

            var model = new UserListViewModel
            {
                Users = users.Items,
                Roles = roles
            };
            return View(model);

        }

        public async Task<ActionResult> EditUserModal(long userId)
        {
            var user = await _userAppService.Get(new EntityDto<long>(userId));
            var roles = (await _userAppService.GetRoles()).Items;
            var organizationunit = await _userAppService.GetUserOrg(userId);
            var userpost = _userAppService.GetUserPost(userId).Result;

            var model = new EditUserModalViewModel
            {
                User = user,
                Roles = roles,
                OrganizationUnitId = organizationunit == null ? 0 : organizationunit.OrganizationUnitId,
                PostId = userpost == null ? 0 : userpost.PostId
            };
            return View("_EditUserModal", model);
        }


        public async Task<ActionResult> EditPostOrg(long userId)
        {
            var user = await _userAppService.Get(new EntityDto<long>(userId));
            var roles = (await _userAppService.GetRoles()).Items;
            var organizationunit = await _userAppService.GetUserOrg(userId);
            var userpost = _userAppService.GetUserPost(userId).Result;

            var model = new EditUserModalViewModel
            {
                User = user,
                Roles = roles,
                OrganizationUnitId = organizationunit == null ? 0 : organizationunit.OrganizationUnitId,
                PostId = userpost == null ? 0 : userpost.PostId
            };
            return View("_EditPostOrg", model);
        }

        public async Task<ActionResult> UserPostAndOrg(int page)
        {

            int pagenumber = page < 1 ? 1 : page;

            GetUsersInput input = new GetUsersInput
            {
                SkipCount = (pagenumber - 1) * AppConsts.DefaultPageSize,
                MaxResultCount = AppConsts.DefaultPageSize
            };
            var users = (await _userAppService.GetUserPostOrgByDapper(input)); // Paging not implemented yet

            var TotalCount = await _userAppService.GetUserCount();

            var PageOfUserListDto = new StaticPagedList<UserPostOrgListDto>(users.Items, pagenumber, AppConsts.DefaultPageSize, TotalCount);

            //将分页结果放入ViewBag供View使用
            ViewBag.PageOfUserListDto = PageOfUserListDto;
            return View();

        }
    }
}
